<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="WD">





<title>C51单片机学习笔记(三)——按键对灯、蜂鸣器等器件的控制 | WD&#39;s blog</title>



    <link rel="icon" href="/favicon1.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    
    <script src="/js/snow.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 5.4.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">WD&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">WD&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">C51单片机学习笔记(三)——按键对灯、蜂鸣器等器件的控制</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/about/">WD</a>
                     &nbsp;

                    
                        <span class="post-time">
                        Date: <a href="#">March 29, 2019&nbsp;&nbsp;17:24:17</a>
                        </span>
                     &nbsp;
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/C51%E5%8D%95%E7%89%87%E6%9C%BA/">C51单片机</a>
                            
                        </span>
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <br>
    <span id="busuanzi_container_site_pv">总阅读量:<a href="#"><span id="busuanzi_value_page_pv"></span></a>次</span>&nbsp;
    <span class="post-count">文章字数:<a href="#">1.4k</span></a>&nbsp;
     <span class="post-count">阅读时长:<a href="#">6</span>min</a>
                    
                </div>
            
        </header>

        <div class="post-content">
            <div id="article_content" class="article_content clearfix">
<div class="toc"><h2><a name="t1"></a>文章目录</h2><ul><ul><ul><li><a href="#1_2" rel="nofollow" data-token="915387045969d814c33767905becd324" target="_self">1.独立按键的原理</a></li><li><a href="#2_10" rel="nofollow" data-token="597486ac1e80e2aa6f19f13c7136fe43" target="_self">2.独立按键控制蜂鸣器响</a></li><li><a href="#3_50" rel="nofollow" data-token="f0c7d0065369bf80581311ba48025981" target="_self">3.独立按键对流水灯的控制</a></li></ul></ul></ul></div><p></p>
<h2><a name="t2"></a><a id="1_2"></a>1.独立按键的原理</h2>
<ul>
<li>一般的独立按键有四个脚，无论按键是否被按下，1和2总是相通的，3和4也是相通的。当按键按下时，1、2脚与3、4脚接通，按住不放则保持接通状态<br>
<img src="https://img-blog.csdnimg.cn/2019032916305167.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li>
<li>按键通断过程的抖动：当按键按下时（释放时）输出的理想波形是标准的矩形波，但是，由于机械触点的弹性作用，闭合时电路不会马上稳定的接通，这中间会出现一连串的抖动，如图所示，抖动时间一般为5~10ms<br>
<img src="https://img-blog.csdnimg.cn/2019032916310050.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li>
<li>按键的消抖：按键的抖动会造成一次按下被误认为多次按下，所以要进行消抖处理，一般分为硬件消抖和软件消抖，这里讨论软件消抖，方法是当单片机检测到按键闭合时，采用延时程序产生5~10ms的延时，等抖动消失后，再判断是否处于闭合状态，同理释放的时候，也要给延时。</li>
<li>按键给单片机传达指令的基本原理：按键的一端接地，另一断接单片机的任意一个I/O口，当按键没被按下时，单片机的I/O口是高电平，当按键被按下时，就变成低电平，此时单片机就认为按键被按下了。<br>
<img src="https://img-blog.csdnimg.cn/20190329163347579.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li>
</ul>
<h2><a name="t3"></a><a id="2_10"></a>2.独立按键控制蜂鸣器响</h2>
<ul>
<li>蜂鸣器的特点：蜂鸣器按内部是否有震荡器可分为有源蜂鸣器和无源蜂鸣器（声音频率可控，可以控制发出音节）</li>
<li>蜂鸣器的驱动电路：</li>
<li><img src="https://img-blog.csdnimg.cn/2019032916364617.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMTgxNTky,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li>
</ul>
<blockquote>
<ul>
<li>图中是PNP型三极管Q2，当P1.5输出低电平时，三极管饱和导通，蜂鸣器鸣响，反之，输出高电平时，三极管截止，蜂鸣器不响。</li>
</ul>
</blockquote>
<ul>
<li>下面编写按键控制蜂鸣器的代码：</li>
</ul>

<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;reg52.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> uint unsigned int 	<span class="comment">//用uint替换unsigned int</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> uchar unsigned char	<span class="comment">//用uchar替换unsigned char</span></span></span><br><span class="line">sbit bell = P1^<span class="number">5</span>;		   <span class="comment">//用P1.5监听蜂鸣器</span></span><br><span class="line">sbit S1 = P3^<span class="number">7</span>;			   <span class="comment">//用P3.7来检测按键是否被按下</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">delay</span><span class="params">(uint z)</span></span>;			   <span class="comment">//延时函数</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	uchar i;</span><br><span class="line">	uint k;</span><br><span class="line">	<span class="keyword">if</span>(S1 == <span class="number">0</span>)     <span class="comment">//第一次检测按键被按下</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">delay</span>(<span class="number">8</span>);   <span class="comment">//延时8ms进行消抖</span></span><br><span class="line">		<span class="keyword">if</span>(S1 == <span class="number">0</span>)	  <span class="comment">//再次检测按键被按下，说明确实被按下了</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)  <span class="comment">//for循环控制蜂鸣器响3声</span></span><br><span class="line">			&#123;	</span><br><span class="line">				<span class="keyword">for</span>(k=<span class="number">0</span>;k&lt;<span class="number">50000</span>;k++);<span class="comment">//延时500ms</span></span><br><span class="line">				bell = <span class="number">0</span>;		     <span class="comment">//输出低电平，使蜂鸣器鸣叫</span></span><br><span class="line">				<span class="keyword">for</span>(k=<span class="number">0</span>;k&lt;<span class="number">50000</span>;k++); <span class="comment">//延时500ms</span></span><br><span class="line">				bell = <span class="number">1</span>;			<span class="comment">//输出高电平，使蜂鸣器停止</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">delay</span><span class="params">(uint z)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	uint x,y;</span><br><span class="line">	<span class="keyword">for</span>(x=z;x&gt;<span class="number">0</span>;x--)</span><br><span class="line">		<span class="keyword">for</span>(y=<span class="number">120</span>;y&gt;<span class="number">0</span>;y--);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2><a name="t4"></a><a id="3_50"></a>3.独立按键对流水灯的控制</h2>
<ul>
<li>编程思路：根据前面按键的基本原理，只需要检测到相应按键按下时，将对应得流水灯点亮（其引脚输出低电平），这样就可以通过按键控制流水灯了。</li>
</ul>

<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;reg52.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> uint unsigned int 	<span class="comment">//用uint替换unsigned int</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> uchar unsigned char	<span class="comment">//用uchar替换unsigned char</span></span></span><br><span class="line">sbit bell = P1^<span class="number">5</span>;		   <span class="comment">//用P1.5监听蜂鸣器</span></span><br><span class="line">sbit S1 = P3^<span class="number">7</span>;			   <span class="comment">//用P3.7来检测按键是否被按下</span></span><br><span class="line">sbit S2 = P3^<span class="number">6</span>;</span><br><span class="line">sbit S3 = P3^<span class="number">5</span>;</span><br><span class="line">sbit S4 = P3^<span class="number">4</span>;</span><br><span class="line">sbit S5 = P3^<span class="number">3</span>;</span><br><span class="line">sbit S6 = P3^<span class="number">2</span>;</span><br><span class="line">sbit LED1 = P2^<span class="number">0</span>;</span><br><span class="line">sbit LED2 = P2^<span class="number">1</span>;</span><br><span class="line">sbit LED3 = P2^<span class="number">2</span>;</span><br><span class="line">sbit LED4 = P2^<span class="number">3</span>;</span><br><span class="line">sbit LED5 = P2^<span class="number">4</span>;</span><br><span class="line">sbit LED6 = P2^<span class="number">5</span>;</span><br><span class="line">sbit LED7 = P2^<span class="number">6</span>;</span><br><span class="line">sbit LED8 = P2^<span class="number">7</span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">delay</span><span class="params">(uint z)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	uchar i;</span><br><span class="line">	uint k;</span><br><span class="line">	<span class="keyword">if</span>(S1 == <span class="number">0</span>)     <span class="comment">//第一次检测按键被按下</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">delay</span>(<span class="number">8</span>);   <span class="comment">//延时8ms进行消抖</span></span><br><span class="line">		<span class="keyword">if</span>(S1 == <span class="number">0</span>)	  <span class="comment">//再次检测按键被按下，说明确实被按下了</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)  <span class="comment">//for循环控制蜂鸣器响3声</span></span><br><span class="line">			&#123;	</span><br><span class="line">				<span class="keyword">for</span>(k=<span class="number">0</span>;k&lt;<span class="number">5000</span>;k++);<span class="comment">//延时500ms</span></span><br><span class="line">				bell = <span class="number">0</span>;		     <span class="comment">//输出低电平，使蜂鸣器鸣叫</span></span><br><span class="line">				<span class="keyword">for</span>(k=<span class="number">0</span>;k&lt;<span class="number">10000</span>;k++); <span class="comment">//延时500ms</span></span><br><span class="line">				bell = <span class="number">1</span>;			<span class="comment">//输出高电平，使蜂鸣器停止</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(S2 == <span class="number">0</span>)     <span class="comment">//第一次检测按键被按下</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">delay</span>(<span class="number">8</span>);   <span class="comment">//延时8ms进行消抖</span></span><br><span class="line">		<span class="keyword">if</span>(S2 == <span class="number">0</span>)	  <span class="comment">//再次检测按键被按下，说明确实被按下了</span></span><br><span class="line">		&#123;</span><br><span class="line">			LED1 = <span class="number">0</span>;     <span class="comment">//正向流动</span></span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED1 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED2 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED2 = <span class="number">1</span>;</span><br><span class="line">			</span><br><span class="line">			LED3 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED3 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED4 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED4 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED5 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED5 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED6 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED6 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED7 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED7 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED8 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED8 = <span class="number">1</span>;		</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(S3 == <span class="number">0</span>)     <span class="comment">//第一次检测按键被按下</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">delay</span>(<span class="number">8</span>);   <span class="comment">//延时8ms进行消抖</span></span><br><span class="line">		<span class="keyword">if</span>(S3 == <span class="number">0</span>)	  <span class="comment">//再次检测按键被按下，说明确实被按下了</span></span><br><span class="line">		&#123;</span><br><span class="line">			LED8 = <span class="number">0</span>;       <span class="comment">//反向流动</span></span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED8 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED7 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED7 = <span class="number">1</span>;</span><br><span class="line">			</span><br><span class="line">			LED6 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED6 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED5 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED5 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED4 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED4 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED3 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED3 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED2 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED2 = <span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">			LED1 = <span class="number">0</span>;</span><br><span class="line">			<span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">			LED1 = <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(S4 == <span class="number">0</span>)     <span class="comment">//第一次检测按键被按下</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">delay</span>(<span class="number">8</span>);   <span class="comment">//延时8ms进行消抖</span></span><br><span class="line">		<span class="keyword">if</span>(S4 == <span class="number">0</span>)	  <span class="comment">//再次检测按键被按下，说明确实被按下了</span></span><br><span class="line">		&#123;</span><br><span class="line">			LED1 = <span class="number">0</span>;   <span class="comment">//1、3、5、7亮</span></span><br><span class="line">			LED2 = <span class="number">1</span>;</span><br><span class="line">			LED3 = <span class="number">0</span>;</span><br><span class="line">			LED4 = <span class="number">1</span>;</span><br><span class="line">			LED5 = <span class="number">0</span>;</span><br><span class="line">			LED6 = <span class="number">1</span>;</span><br><span class="line">			LED7 = <span class="number">0</span>;</span><br><span class="line">			LED8 = <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(S5 == <span class="number">0</span>)     <span class="comment">//第一次检测按键被按下</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">delay</span>(<span class="number">8</span>);   <span class="comment">//延时8ms进行消抖</span></span><br><span class="line">		<span class="keyword">if</span>(S5 == <span class="number">0</span>)	  <span class="comment">//再次检测按键被按下，说明确实被按下了</span></span><br><span class="line">		&#123;</span><br><span class="line">			LED1 = <span class="number">1</span>;    <span class="comment">//2、4、6、8亮</span></span><br><span class="line">			LED2 = <span class="number">0</span>;</span><br><span class="line">			LED3 = <span class="number">1</span>;</span><br><span class="line">			LED4 = <span class="number">0</span>;</span><br><span class="line">			LED5 = <span class="number">1</span>;</span><br><span class="line">			LED6 = <span class="number">0</span>;</span><br><span class="line">			LED7 = <span class="number">1</span>;</span><br><span class="line">			LED8 = <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(S6 == <span class="number">0</span>)     <span class="comment">//第一次检测按键被按下</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">delay</span>(<span class="number">8</span>);   <span class="comment">//延时8ms进行消抖</span></span><br><span class="line">		<span class="keyword">if</span>(S6 == <span class="number">0</span>)	  <span class="comment">//再次检测按键被按下，说明确实被按下了</span></span><br><span class="line">		&#123;</span><br><span class="line">			LED1 = <span class="number">1</span>;  <span class="comment">//全部灯灭</span></span><br><span class="line">			LED2 = <span class="number">1</span>;</span><br><span class="line">			LED3 = <span class="number">1</span>;</span><br><span class="line">			LED4 = <span class="number">1</span>;</span><br><span class="line">			LED5 = <span class="number">1</span>;</span><br><span class="line">			LED6 = <span class="number">1</span>;</span><br><span class="line">			LED7 = <span class="number">1</span>;</span><br><span class="line">			LED8 = <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">delay</span><span class="params">(uint z)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	uint x,y;</span><br><span class="line">	<span class="keyword">for</span>(x=z;x&gt;<span class="number">0</span>;x--)</span><br><span class="line">		<span class="keyword">for</span>(y=<span class="number">120</span>;y&gt;<span class="number">0</span>;y--);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
                    </div>
        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span><a href="/about/">WD</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://did321.gitee.io/2019/03/29/C51%E5%8D%95%E7%89%87%E6%9C%BA%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%89-%E2%80%94%E2%80%94%E6%8C%89%E9%94%AE%E5%AF%B9%E7%81%AF%E3%80%81%E8%9C%82%E9%B8%A3%E5%99%A8%E7%AD%89%E5%99%A8%E4%BB%B6%E7%9A%84%E6%8E%A7%E5%88%B6/">https://did321.gitee.io/2019/03/29/C51%E5%8D%95%E7%89%87%E6%9C%BA%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%89-%E2%80%94%E2%80%94%E6%8C%89%E9%94%AE%E5%AF%B9%E7%81%AF%E3%80%81%E8%9C%82%E9%B8%A3%E5%99%A8%E7%AD%89%E5%99%A8%E4%BB%B6%E7%9A%84%E6%8E%A7%E5%88%B6/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>Copyright (c) 2022 <a target="_blank" rel="noopener" href="http://creativecommons.org/licenses/by-nc/4.0/">CC-BY-NC-4.0</a> LICENSE</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span><a href="#">The blog is my giant.</a></span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/C51%E5%8D%95%E7%89%87%E6%9C%BA/"># C51单片机</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2019/04/05/C51%E5%8D%95%E7%89%87%E6%9C%BA%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E5%9B%9B-%E2%80%94%E2%80%94%E4%B8%AD%E6%96%AD%E7%B3%BB%E7%BB%9F%E5%8F%8A%E5%BA%94%E7%94%A8/">C51单片机学习笔记(四)——中断系统及应用</a>
            
            
            <a class="next" rel="next" href="/2019/03/28/C51%E5%8D%95%E7%89%87%E6%9C%BA%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C-%E2%80%94%E2%80%94%E8%8A%B1%E6%A0%B7%E6%B5%81%E6%B0%B4%E7%81%AF%E7%9A%84%E5%AE%9E%E7%8E%B0/">C51单片机学习笔记(二)——花样流水灯的实现</a>
            
        </section>
        <br>
        <br>
    
    <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
    <div id="vcomments"></div>
    <script>
        new Valine({
    el: '#vcomments' ,

    appId: 'JvFy3ebVLo2rUYgHaMweJyXX-MdYXbMMI',
    appKey: 'TCFxfjDAM8UmERPEgYXJmT40',
    serverURLs: 'https://JvFy3ebV.api.lncldglobal.com', 
    placeholder: '----评论区----留下你的评论，作者会定期回复！在昵称处填写QQ号可自动获取邮箱和QQ头像（保护QQ邮箱隐私）',
    enableQQ: true,
    requiredFields: ['nick'],
});
    </script>

    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© WD | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>

</html>
